﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Com.Seezt.Skins;
using Components;
using UI.Lib;
using UI.Lib.Factory;
using System.Collections;

namespace UI.StorageManagement
{
    public partial class FrmSto6a00Medit : UI.ParentForm.BaseMedit
    {
        ToolStripButton btnReset = null;
        public FrmSto6a00Medit(string modulecode, string action, string text, string key)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.Text = text;
            this.Key = key;

            //回写的方法
            this.SaveCallFuntion = "Sto6a00Manager$MeditSaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            base.MaxStateFlag = false;
            this.allowDtlEmpty = false;
            IsAfterSaveCloseWin = true;
            SetToolButton();
            ButtonStateInit(false);
            this.Size = base.LargeWindow;
        }

        private void SetToolButton()
        {
            tspDtlTop.Items.Insert(0, CreateToolBtn("T", "提取仓库库存", btnGetData_Click));
            btnReset = CreateToolBtn("R", "重 置", btnReset_Click);
            tspDtlTop.Items.Add(btnReset);
        }

        protected override void PageInit()
        {
            tspSave.Visible = true;
        }

        private void ButtonStateInit(bool enabled = true)
        {
            tspBtnAdd.Enabled = enabled;
            tspBtnDel.Enabled = enabled;
            btnReset.Enabled = enabled;
        }

        void btnGetData_Click(object sender, EventArgs e)
        {
            if (AutoCheck() == false)
                return;

            this.tbSummary.Enable("stocode", false);
            var storageid = this.tbSummary.GetValue("storageid");

            ButtonStateInit();

            QB where = QB.Entity.AddParam("@comcode", Context.realComcode)
                                .AddParam("@storageid", storageid);

            //加载顶层节点
            var data = CommonHelper.Entity.ExecFun("sto6a00meditdata", where);

            BindEditData(data);
        }

        void btnReset_Click(object sender, EventArgs e)
        {
            this.dgvDtl.Rows.Clear();
            this.tbSummary.Enable("stocode", true);
            ButtonStateInit(false);
        }

        protected override bool BeforeDelete(int rowIndex)
        {
            var isAddRow = Validation.IsNull(this.dgvDtl["listid", rowIndex].Value);

            if (isAddRow == false)
            {
                MsgBox.Show("您不能删除已有的库存记录。");
                return false;
            }

            return true;
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if (Validation.IsNull(this.dgvDtl.GetValue("listid")) == false)
                return false;

            if ("productcode" == name)
            {
                where = string.Format(" not exists(select 1 from sto6a00 b where a.listid = b.pid and b.storageid='{1}') {0}",
                            Context.GetAppKeyWhere("a.muser", true),
                            this.tbSummary.GetValue("storageid")
                        );
            }

            return true;
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("stocode" == name)
            {
                where = Context.GetAppKeyWhere("usercode");
            }

            return true;
        }
    }
}
